package Tetris;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import javax.swing.KeyStroke;

public class tem_a {
    public static void main(String[] args) {
        JFrame frame = new JFrame("CardLayout 示例");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(400, 300);

        // 创建主面板并设置 CardLayout 布局
        JPanel cardPanel = new JPanel(new CardLayout());

        // 创建两个面板（界面）
        JPanel panel1 = createPanel("面板 1", Color.LIGHT_GRAY);
        JPanel panel2 = createPanel("面板 2", Color.PINK);

        // 将面板添加到 cardPanel 中，并为每个面板指定一个名称
        cardPanel.add(panel1, "PANEL1");
        cardPanel.add(panel2, "PANEL2");

        // 创建按钮面板
        JPanel buttonPanel = new JPanel();
        JButton btn1 = new JButton("显示面板 1");
        JButton btn2 = new JButton("显示面板 2");

        // 为按钮添加事件监听器
        btn1.addActionListener(e -> {
            CardLayout cl = (CardLayout) cardPanel.getLayout();
            cl.show(cardPanel, "PANEL1"); // 显示名为 "PANEL1" 的面板
        });

        btn2.addActionListener(e -> {
            CardLayout cl = (CardLayout) cardPanel.getLayout();
            cl.show(cardPanel, "PANEL2"); // 显示名为 "PANEL2" 的面板
        });

        buttonPanel.add(btn1);
        buttonPanel.add(btn2);

        // 将按钮面板和 cardPanel 添加到框架中
        frame.getContentPane().add(buttonPanel, BorderLayout.NORTH);
        frame.getContentPane().add(cardPanel, BorderLayout.CENTER);

        frame.setVisible(true);
    }

    // 辅助方法：创建带有标签的面板
    private static JPanel createPanel(String title, Color background) {
        JPanel panel = new JPanel();
        panel.setBackground(background);
        panel.add(new JLabel(title, JLabel.CENTER));
        return panel;
    }
}
